package com.swa99ylum.pjv.sample.s01_nodeframe.s0120;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.swa99ylum.pjv.Fuse;
import com.swa99ylum.pjv.PluggableJSONValidator;
import com.swa99ylum.pjv.bean.ArrayJSONNode;
import com.swa99ylum.pjv.bean.JSONNode;
import com.swa99ylum.pjv.bean.ObjectJSONNode;
import com.swa99ylum.pjv.corrector.DateFormatCorrector;
import com.swa99ylum.pjv.sample.FileUtils;
import com.swa99ylum.pjv.validator.DateFormatValidator;
import com.swa99ylum.pjv.validator.InclusionValidator;

public class Test1 {
	public static void main(String[] args) throws Exception {
		String filePath = Test1.class.getResource("json_0120.txt").getPath();
		
		//step1、获取 json
		String json = null;
		try {
			json = FileUtils.getFileContent(filePath);
		} catch (Exception e) {
			System.err.println("从 文件中，读取 json 有误");
		}
		
		System.out.println(json);
		
		
		//step2、定义 json 的 nodeFrame
		@SuppressWarnings("serial")
		Map<String, Object[]> nodeFrame = new HashMap<String, Object[]>(){{
			put("$[O]", new Object[]{
					new JSONNode("name\\[O\\]"), 
					new JSONNode("teacher"), 
					new JSONNode("c_date", new DateFormatValidator("YYYY/MM/dd")).setCorrector(new DateFormatCorrector("yyyy-MM-dd", "yyyy/MM/dd")), 
					new JSONNode("grade", new InclusionValidator(new String[]{"asd", "1-"})), 
					new ObjectJSONNode("tv\\[\\.1\\]"), 
					new ArrayJSONNode("student\\[O\\]")});
			
			put("$[O].tv\\[\\.1\\][O]", new Object[]{
					new JSONNode("brand"), 
					new JSONNode("price"), 
					new JSONNode("size111")});
			
			put("$[O].student\\[O\\][A]", new Object[]{
					new JSONNode("id"), 
					new JSONNode("name"), 
					new JSONNode("age"), 
					new ArrayJSONNode("pet")});
			
			put("$[O].student\\[O\\][A].pet[A]", new Object[]{
					new JSONNode("name"), 
					new JSONNode("type")});
			
		}};
		
		//step3、定义 errorMap，用于存储 验证出来的 错误结果
		Map<String, String> errorMap = new HashMap<String, String>();
		
		//step4、验证 json
		PluggableJSONValidator p = new PluggableJSONValidator(json, nodeFrame, Fuse.OBJECT, errorMap);
		p.parse();
		
		for(Entry<String, String> entry : errorMap.entrySet()) {
			System.out.println(entry.getKey() + " => " + entry.getValue());
		}
		
		System.out.println(p.getRootStrPretty());
		
	}

}
